﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ninject.Activation;
using Ninject.Syntax;

namespace Reap.Common.Runtime.Dependency.Ninject.InRequestScope {
	/// <summary>
	/// 请求作用域方法扩展
	/// </summary>
	public static class RequestScopeExtensionMethod {

		/// <summary>
		/// 获取请求作用域 是HTTP还是线程
		/// </summary>
		/// <param name="ctx"></param>
		/// <returns></returns>
		private static object GetScope(IContext ctx) {
			return ((object)System.Web.HttpContext.Current ?? (object)System.Threading.Thread.CurrentThread);
		}

		/// <summary>
		/// 返回当前实例的作用域范围
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="syntax"></param>
		/// <returns></returns>
		public static IBindingNamedWithOrOnSyntax<T> InRequestScope<T>(this IBindingInSyntax<T> syntax) {
			return syntax.InScope(new Func<IContext, object>(RequestScopeExtensionMethod.GetScope));
		}
	}
}
